package com.typhoon.spring_shiro.utils.encrypt;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

public class AESUtil {
    private final static String IVKEY  = "9greqde7banhkxfv";
    private static       Cipher cipher = null;

    public static byte[] encrypt(String data, String key) {
        try {

            cipher = Cipher.getInstance("AES/CBC/NoPadding");
            int             blockSize = cipher.getBlockSize();
            SecretKeySpec   keyspec   = new SecretKeySpec(fullZore(key, blockSize), "AES");
            IvParameterSpec ivspec    = new IvParameterSpec(fullZore(IVKEY, blockSize));
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(fullZore(data, blockSize));
            return encrypted;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static byte[] decrypt(byte[] data, String key) {
        try {
            Cipher          cipher    = Cipher.getInstance("AES/CBC/NoPadding");
            int             blockSize = cipher.getBlockSize();
            SecretKeySpec   keyspec   = new SecretKeySpec(fullZore(key, blockSize), "AES");
            IvParameterSpec ivspec    = new IvParameterSpec(fullZore(IVKEY, blockSize));
            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            byte[] decrypted = cipher.doFinal(data);
            return decrypted;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static byte[] fullZore(String data, int blockSize) {
        byte[] dataBytes       = data.getBytes();
        int    plaintextLength = dataBytes.length;
        if (plaintextLength % blockSize != 0) {
            plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));

        }
        byte[] plaintext = new byte[plaintextLength];
        System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
        return plaintext;
    }

    public static void main(String[] args) {

        String content = "222222";
        String pwds    = "ZGQ3MTAwYmY0NGJj";
        // TWurfZeHNgM2dQSZMDOWYw==
        // :Yfd+baBFAv9hCHBd+Mxp/g==

        System.out.println(content);
        byte[] encryptResultStr = encrypt(content, pwds);
        // System.out.println("���ܺ�" + new String(encryptResultStr));

        System.out.println("base64EncodeResult:" + Base64.encodeBase64String(encryptResultStr));
        byte[] decryptString = decrypt(encryptResultStr, pwds);
        System.out.println(new String(decryptString));

    }

    public static void main2(String[] args) {

        String content       = "TWurfZeHNgM2dQSZMDOWYw==";
        String pwds          = "r593iole95han3g5";
        byte[] decode64      = Base64.decodeBase64(content);
        byte[] decryptString = decrypt(decode64, pwds);
        System.out.println(new String(decryptString));

    }

}